package com.example.yuntiku.Servlet;

import com.alibaba.fastjson.JSON;
import com.example.yuntiku.Service.CurriculumService;
import com.example.yuntiku.Service.RecordService;
import com.example.yuntiku.dao.UserDao;
import com.example.yuntiku.dao.impl.TeacherDao;
import com.example.yuntiku.dao.impl.UsersDao;
import com.example.yuntiku.entity.Curriculum;
import com.example.yuntiku.entity.Teacher;
import com.example.yuntiku.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "CurriculumServlet", value = "/CurriculumServlet")
public class CurriculumServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//        String selectedOne = request.getParameter("one");
//        if (selectedOne == null) {
//            selectedOne = "2"; // 设置一个默认值，默认为2
//        }
//        CurriculumService curriculumService = new CurriculumService();
//        List<Curriculum> list = curriculumService.getCurriculums();
//        List<Curriculum> list1 = curriculumService.two(Integer.parseInt(selectedOne));
//        request.setAttribute("curriculums", list);
////        request.setAttribute("list1", list1);
//        request.getRequestDispatcher("shuccs.jsp").forward(request, response);
//
////        response.setCharacterEncoding("utf-8");


        User u = (User) request.getSession().getAttribute("user");
        UsersDao usersDao=new UsersDao();
        User user=usersDao.selectById(u.getUid());
        CurriculumService curriculumService = new CurriculumService();
        List<Curriculum> list = curriculumService.selectAll2();
        for (Curriculum curriculum : list) {
            List<Curriculum> list1 = curriculumService.selectZji(curriculum.getCid());
            for (Curriculum curriculum1 : list1) {
                List<Curriculum> list2 = curriculumService.selectZji(curriculum1.getCid());
                curriculum1.setChildren(list2);
            }
            curriculum.setChildren(list1);
        }
        List<Curriculum> list1 = curriculumService.selecterjilei();
        List<Integer> list2 = new ArrayList<>();
        int i=0;
        for (Curriculum curriculum : list1) {
            RecordService recordService = new RecordService();
            i = recordService.selectJindu(curriculum.getCid());
            if(i!=0){
                list2.add(recordService.selectJindu(curriculum.getCid()));
            }else{
                list2.add(recordService.selectJindu(0));
            }
        }
        request.setAttribute("jindu",list2);
        request.setAttribute("curriculums",list);
        request.getRequestDispatcher("zhuym.jsp").forward(request,response);
    }
}